package dao;


import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

//1.新增用户
//2.根据用户名查找用户
//3.根据用户id查找用户
public class UserDao {
    //新增用户
    public static void insert(User user){
        Connection connection=null;
        PreparedStatement statement=null;
        try {
            //建立连接
            connection=DBUtil.connection();
            //进行sql语句拼装
            String sql="insert into user values(null,?,?)";
            statement=connection.prepareStatement(sql);
            statement.setString(1,user.getUsername());
            statement.setString(2,user.getPassword());
            int k= statement.executeUpdate();
            if(k==1){
                System.out.println("成功");
            }else {
                System.out.println("失败");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DBUtil.close(connection,statement,null);
        }
    }
    //通过用户名进行查询
    public static User selectByName(String username) {
        Connection connection=null;
        PreparedStatement statement=null;
        ResultSet resultSet=null;
        User user=new User();
        try {
            connection=DBUtil.connection();
            //拼装字符串
            String sql="select * from user where username=?";
            statement=connection.prepareStatement(sql);
            statement.setString(1,username);
            resultSet= statement.executeQuery();
            if (resultSet.next()) {
//                System.out.println("找到啦！");
                user.setUserId(resultSet.getInt("userId"));
                user.setUsername(resultSet.getString("username"));
                user.setPassword(resultSet.getString("password"));
            }else {
                System.out.println("没找到");
                User use=new User();
                use.setUsername("undefine");
                return  use;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DBUtil.close(connection,statement,resultSet);
        }
        return user;
    }
    //根据用户id进行查询
    public static User selectById(int userId){
        Connection connection=null;
        PreparedStatement statement=null;
        ResultSet resultSet=null;
        User user=new User();
        try {
            connection=DBUtil.connection();
            //拼装字符串
            String sql="select * from user where userId=?";
            statement=connection.prepareStatement(sql);
            statement.setInt(1,userId);
            resultSet= statement.executeQuery();
            if (resultSet.next()) {
//                System.out.println("找到啦！");
                user.setUserId(resultSet.getInt("userId"));
                user.setUsername(resultSet.getString("username"));
                user.setPassword(resultSet.getString("password"));
            }else {
                System.out.println("没找到");
                User use=new User();
                use.setUsername("undefine");
                return  use;
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            DBUtil.close(connection,statement,resultSet);
        }
        return user;
    }

//    public static void main(String[] args) {
//        UserDao userDao=new UserDao();
//        User user=new User();
//        user.setPassword("123");
//        user.setUsername("456");
//        userDao.insert(user);
//    }
}
